
<%@ page import="coe.pumbaa.Comment" %>
<% def springSecurityService = application.getAttribute("org.codehaus.groovy.grails.APPLICATION_CONTEXT").getBean("springSecurityService") %>
<% def stringManagerService = application.getAttribute("org.codehaus.groovy.grails.APPLICATION_CONTEXT").getBean("stringManagerService") %>
<% def authenticationManagerService = application.getAttribute("org.codehaus.groovy.grails.APPLICATION_CONTEXT").getBean("authenticationManagerService") %>

<!doctype html>
<html>
  <head>
    <meta name="layout" content="PumbaaLayout">
  <g:set var="entityName" value="${message(code: 'comment.label', default: 'Comment')}" />
  <title>Post ${postInstance.title}</title>
  <script>

    function addPicTag() {
      document.commentForm.message.value = document.commentForm.message.value+'[img]url picture[/img]';
    }
  </script>
</head>
<body>

  <section id="main" class="column">
    <pumbaa:alert />
    <article class="module width_full">
      <header><h3>${postInstance.title}</h3></header>
      <table>
        <tr>
          <td style="text-align: center;">
${postInstance?.owner?.displayName}<br>
        <img src="${createLink(controller:'user', action:'viewImage', id:postInstance?.owner?.id)}" width="100" height="132"><br>
        <g:if test="${springSecurityService.isLoggedIn()}">
        <table><tr>
        <g:render template="/post/action" model="['post':postInstance]"/>
        </tr></table>
        </g:if>
          </td>
          <td>
${stringManagerService.stringToHTML(postInstance?.description)}<br>
${stringManagerService.stringToHTML(postInstance?.owner?.signature)}
          </td>
        </tr>
      </table>
      <footer>
        <!-- Add likes , date and delete -->
        <g:if test="${springSecurityService.isLoggedIn()}">
        <table style="float: right;">
          <tr>
            <td>
        <div id="show_subscribe${postInstance.id}">
          <g:render template="/post/subscribe" model="['postInstance',postInstance]"/>
        </div>
            </td>
            <td>
        <div id="show_like">
          <g:render template="/post/like" model="['postInstance',postInstance]"/>
        </div>
            </td>
        </tr>
        </table>
        </g:if>
      </footer>
    </article>

    <g:each in="${commentInstanceList}" var="comment" status="i">
      <article class="module comment_width">
        <header><h3>Comment date : <g:formatDate format="dd/MM/yyyy ( HH:mm:ss )" date="${comment.timestamp}"/></h3></header>
        <table>
          <tr>
            <td style="text-align: center;">
${comment.owner.displayName}<br>
          <img src="${createLink(controller:'user', action:'viewImage', id:comment.owner.id)}" width="50" height="66"><br>
          <g:if test="${springSecurityService.isLoggedIn()}">
          <g:render template="action" model="['comment':comment]"/>
          </g:if>
            </td>
            <td>
${stringManagerService.stringToHTML(comment.message)}
            </td>
          </tr>
        </table>
        <footer>
          <!-- Add likes for anyone and delete for admin-->
          <g:if test="${springSecurityService.isLoggedIn()}">
          <div id="show_like_comment${comment?.id}" style="text-align: right;margin-top: 5px;">
            <g:render template="like" model="['comment':comment]"/>
          </div>
          </g:if>
        </footer>
      </article>
    </g:each>
    
    <g:if test="${commentInstanceTotal > 10}">
        <article class="module comment_width">
      <div class="pagination">
              <g:paginate id="${postInstance.id}" total="${commentInstanceTotal}" />
            </div>
        </article>
    </g:if>
    
    <article class="module comment_width">
      <header><h3>Comment</h3></header>
      <table style="width: 100%;">
        <tr>
          <td style="text-align: center; width: 110px;">
            <g:if test="${springSecurityService.isLoggedIn()}">
${springSecurityService?.currentUser?.displayName}<br>
        <img src="${createLink(controller:'user', action:'viewImage', id:springSecurityService?.currentUser?.id)}" width="100" height="132">
            </g:if><g:else>
              ${authenticationManagerService?.getAnonymousUser().displayName}<br>
              <img src="${createLink(controller:'user', action:'viewImage', id:authenticationManagerService?.getAnonymousUser().id)}" width="100" height="132">
            </g:else>
          </td>
          <td>
        <g:form name="commentForm" action="save" method="POST">
          <input type="hidden" name="postID" value="${postInstance.id}"/>
          <div class="module_content">
            <fieldset>
              <label>Message<input type="button" id="addPic" class="btn_cam" onClick="addPicTag()"/></label>
              <textarea rows="12" name="message"></textarea>
            </fieldset>
            <div class="submit_link">
              <g:actionSubmit class="alt_btn" style="float: left;" value="Reply" action="save" />
            </div>
          </div>
        </g:form>
      </td>
      </tr>
      </table>
      <footer  style="text-align: right;">
        <h4>Now : <g:formatDate format="dd/MM/yyyy" date="${new Date()}"/></h4>
      </footer>
    </article>
    <div class="spacer"></div>
  </section>
</body>
</html>
